package com.dongbao.core.inspection;

import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTBlock;
import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;

public class PmdDemoWhileLoops extends AbstractJavaRule {

    public Object visit(ASTWhileStatement node,Object data){
        Node firstStmt = node.jjtGetChild(1);
        if(!hasBlockAsFirstChild(firstStmt)){
            addViolation(data,node);
        }
        return super.visit(node,data);
    }


    public boolean hasBlockAsFirstChild(Node node){
        return (node.jjtGetNumChildren() != 0 && (node.jjtGetChild(0) instanceof ASTBlock));
    }

}
